package cn.wolfcode.config;

import cn.wolfcode.domain.OrderInfo;
import cn.wolfcode.redis.SeckillRedisKey;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import top.javatool.canal.client.annotation.CanalTable;
import top.javatool.canal.client.handler.EntryHandler;

/**
 * nacal的客户端：用来监听获取某个表（这里是订单表）更新的数据;
 * 业务：将此更新的数据同步到redis中  固定的seckillOrderHash <orderNo,orderInfo>
 */
@Component
@CanalTable("t_order_info")
public class NacalClient implements EntryHandler<OrderInfo> {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public void insert(OrderInfo orderInfo) {
        System.out.println("新增："+orderInfo);
        stringRedisTemplate.opsForHash().put(SeckillRedisKey.SECKILL_ORDER_HASH.getRealKey(""),orderInfo.getOrderNo(), JSON.toJSONString(orderInfo));
    }

    @Override
    public void update(OrderInfo before, OrderInfo after) {
        System.out.println("更新前的:"+before);
        stringRedisTemplate.opsForHash().put(SeckillRedisKey.SECKILL_ORDER_HASH.getRealKey(""),after.getOrderNo(),JSON.toJSONString(after));
        System.out.println("更新后的:"+after);
    }

    @Override
    public void delete(OrderInfo orderInfo) {
        System.out.println("删除的:"+orderInfo);
        stringRedisTemplate.opsForHash().delete(SeckillRedisKey.SECKILL_ORDER_HASH.getRealKey(""),orderInfo.getOrderNo());
    }
}
